﻿using System.Text;
using QFlow.Core;
using System;
using System.Collections.Generic;
using System.IO;

namespace QFlow.Windows.Forms
{
    public class OrderPrinter
    {
        public void PrintOrder(PrintOrder order)
        {
            BixolonPrinter.Printer.ChangeAlignment(Alignment.Center);
            BixolonPrinter.Printer.ChangeFont("FontA2x2", 9.5f, true);
            BixolonPrinter.Printer.PrintString("QFlow");

            BixolonPrinter.Printer.ChangeAlignment(Alignment.Center);
            BixolonPrinter.Printer.ChangeFont("Traditional Arabic", 9.5f, false);

            StringBuilder itemsBuilder = new StringBuilder();

            AddStringToBuilder(itemsBuilder, order.Title, 48, false);
            itemsBuilder.AppendLine();
            AddStringToBuilder(itemsBuilder, string.Format("{0} : {1}", "رقم الفاتورة", order.OrderID), 48, false);
            itemsBuilder.AppendLine();

            itemsBuilder.Append("==============================================");
            itemsBuilder.AppendLine();
            AddTableHeaderString(itemsBuilder);
            itemsBuilder.AppendLine();
            itemsBuilder.Append("=====  =====  ==================================");
            itemsBuilder.AppendLine();

            foreach (var item in order.Items)
            {
                AddItemString(item, itemsBuilder);
                itemsBuilder.AppendLine();
            }

            itemsBuilder.Append("==============================================");
            itemsBuilder.AppendLine();

            AddStringToBuilder(itemsBuilder, string.Format("{0} : {1}", "الأجمالى", order.Value.ToString("F")), 48, true);
            itemsBuilder.AppendLine();

            AddStringToBuilder(itemsBuilder, string.Format("{2} {0} : {1}", "التاريخ والتوقيت", order.Date.ToString("dd-MM-yyyy  hh:mm"), order.Date.ToString("tt")), 48, false);
            itemsBuilder.AppendLine();

            File.WriteAllText("C:\\kiro.txt", itemsBuilder.ToString());
            BixolonPrinter.Printer.PrintString(itemsBuilder.ToString());
            BixolonPrinter.Printer.Flush();
        }

        private void AddTableHeaderString(StringBuilder builder)
        {
            AddStringToBuilder(builder, "الصنف", 35, true);
            AddStringToBuilder(builder, "كمية", 5, true);
            AddStringToBuilder(builder, "سعر", 6, false);
        }

        private void AddItemString(QFlow.Windows.Forms.PrintOrder.PrintItem item, StringBuilder itemsBuilder)
        {
            AddStringToBuilder(itemsBuilder, item.Name, 35, true);
            itemsBuilder.Append("  ");
            AddStringToBuilder(itemsBuilder, item.Quantity.ToString("F"), 5, true);
            itemsBuilder.Append("  ");
            AddStringToBuilder(itemsBuilder, item.ItemPrice.ToString("F"), 5, false);
        }

        private void AddStringToBuilder(StringBuilder builder, string str, int length, bool alignLeft)
        {
            if (alignLeft)
            {
                for (int i = 0; i < (str.Length > length ? length : str.Length); i++)
                {
                    builder.Append(str[i]);
                }
                for (int i = 0; i < (length - str.Length) * 2; i++)
                {
                    builder.Append(' ');
                }
            }
            else
            {
                for (int i = 0; i < (length - str.Length) * 2; i++)
                {
                    builder.Append(' ');
                }
                for (int i = 0; i < (str.Length > length ? length : str.Length); i++)
                {
                    builder.Append(str[i]);
                }
            }
        }
    }

    public class PrintOrder
    {
        public class PrintItem
        {
            public string Name { get; set; }
            public double ItemPrice { get; set; }
            public double Quantity { get; set; }
        }

        public string OrderID { get; set; }
        public string Title { get; set; }
        public DateTime Date { get; set; }
        public double Value { get; set; }
        public List<PrintItem> Items { get; set; }
    }
}
